#ifndef __UART_H__
#define __UART_H__
#include "stm32f4xx.h"
#include "stdbool.h"
#include "easyfifo.h"
#define USE_SEND_LOCK   1   
#if USE_SEND_LOCK
#include "rtos.h"
#endif

#define UART1_EN          1
#define UART2_EN          1
#define UART3_EN          0
#define UART4_EN          0
#define UART5_EN          0
#define UART6_EN          0

#define UART1_TX_BUF_LEN   128
#define UART2_TX_BUF_LEN   128
#define UART3_TX_BUF_LEN   128
#define UART4_TX_BUF_LEN   128
#define UART5_TX_BUF_LEN   128
#define UART6_TX_BUF_LEN   128

#define UART1_RX_FIFO_LEN   512
#define UART1_RX_CNT_FIFO_LEN  10
#define UART2_RX_FIFO_LEN      512
#define UART2_RX_CNT_FIFO_LEN  10
#define UART3_RX_FIFO_LEN   512
#define UART3_RX_CNT_FIFO_LEN  10
#define UART4_RX_FIFO_LEN   512
#define UART4_RX_CNT_FIFO_LEN  10
#define UART5_RX_FIFO_LEN   512
#define UART5_RX_CNT_FIFO_LEN  10
#define UART6_RX_FIFO_LEN   512
#define UART6_RX_CNT_FIFO_LEN  10


#define UART1_RECV_INTERVAL  8  //ms
#define UART2_RECV_INTERVAL  8  //ms
#define UART3_RECV_INTERVAL  8  //ms
#define UART4_RECV_INTERVAL  8  //ms
#define UART5_RECV_INTERVAL  8  //ms
#define UART6_RECV_INTERVAL  8  //ms

typedef struct {
	u32 baudrate;
	u16 word_len;
	u16 stop_bit;
	u16 parity;
}uart_param_t;


typedef struct
{
	USART_TypeDef* uart_num;
	u32 tx_reg_addr;
	DMA_TypeDef *dma_num;
	u32 dma_channel;
	DMA_Stream_TypeDef *dma_stream;
	u32 dma_it_tcif;
	
	u8 *tx_buf;
	u32 tx_buf_len;
	u32 tx_data_len;
	bool is_first_send;
	
	void *lock_handle;
	
	easy_fifo_t *rx_data_fifo;
	easy_fifo_t *rx_cnt_fifo;
	bool en_rx_timeout;
	u8 timer_id;
}uart_struct_t;

#if UART1_EN
	extern uart_struct_t uart_1;
#endif

#if UART2_EN
	extern uart_struct_t uart_2;
#endif

void uart_init(void);
void uart_send(uart_struct_t *uart_x,u8 *buf, u16 len);
void uart_printf(uart_struct_t *uart_x,char* fmt,...);
void uart_send_no_lock(u8 *buf, u16 len, uart_struct_t *uart_x);

void uart_rx_fifo_clean(uart_struct_t *uart_x);
void uart_enable_rx_timeout(uart_struct_t *uart_x);
void uart_disable_rx_timeout(uart_struct_t *uart_x);
bool uart_get_one_rx_timeout_pcket(uart_struct_t *uart_x,u8 *buf,u32 *cnt,u32 max_read);

#endif








